package com.eastfair.auth.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.eastfair.auth.enumeration.OrganizationOrgTypeEnum;
import com.eastfair.auth.service.OrganizationService;
import com.eastfair.auth.dto.OrganizationDTO;
import com.eastfair.auth.entity.Organization;
import com.eastfair.auth.service.AudienceOrganizationService;
import com.eastfair.auth.service.UserOrganizationService;
import com.eastfair.core.base.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 观众组织操作业务实现类
 *
 * </p>
 *
 * @author ligang
 * @date 2021-07-16
 */
@Slf4j
@Service
@DS("#thread.tenant")
public class AudienceOrganizationServiceImpl implements AudienceOrganizationService {

    @Autowired
    OrganizationService organizationService;

    @Autowired
    UserOrganizationService userOrganizationService;

    @Override
    public R<Organization> createAudienceClassify(Long orgId, Long projectId) {
        OrganizationDTO organizationDTO = new OrganizationDTO();
        organizationDTO.setName("项目下的观众分类");
        organizationDTO.setOrgType(OrganizationOrgTypeEnum.P_AUDIENCE_CLASSIFY);
        organizationDTO.setParentId(orgId);
        organizationDTO.setProjectId(projectId);
        return organizationService.createOrg(organizationDTO);
    }

    @Override
    public R addUserOfAudience(Long projectId, List<Long> userIds) {
        //查询观众分类的节点
        Organization organization = organizationService.queryOrgOfType(projectId, OrganizationOrgTypeEnum.P_AUDIENCE_CLASSIFY);
        if (organization != null) {
            userOrganizationService.addUserOfOrg(organization.getId(), userIds, projectId);
        }
        return R.fail("添加失败");
    }

}
